package com.capgemini.wechat.service;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.capgemini.commons.JsonUtil;
import com.capgemini.commons.PurchaseConstants;
import com.capgemini.commons.sftp.SFTPProcessor;
import com.capgemini.commons.sftp.SftpProperties;
import com.capgemini.commons.sftp.ZipFileUtils;
import com.capgemini.remind.service.CommonsService;
import com.capgemini.commons.sftp.SftpFileDto;
import com.capgemini.wechat.exception.ConfigurationException;
import com.jcraft.jsch.ChannelSftp;

/**
 * 
 * @author
 * @date
 */
@Service
public class FileImportBaseService {
	public final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private CommonsService commonsService;

	public static List<SftpFileDto> fileList;
	public static ChannelSftp channelSftp;
	public static SFTPProcessor ftpUtil = new SFTPProcessor();
	public static SftpProperties sftpProperties;

	public void downloadSFTPFile() throws IOException, ParseException, ConfigurationException {
		// 从SFTP上下载文件
		String sftpInfo = commonsService.getCodeValueByCodeTypeAndCode(PurchaseConstants.SERVICE_REMIND_FILE_SYNCHRONIZATION,
				PurchaseConstants.SERVICE_REMIND_FILE_SYNCHRONIZATION);
		if (!"".equals(sftpInfo)) {
			sftpProperties = JsonUtil.fromJson(sftpInfo, SftpProperties.class);
			// 取出所有的文件
			String prefix = commonsService.getCodeValueByCodeTypeAndCode("SRFromMARS", "SRFromMARS");
			channelSftp = ftpUtil.getConnect(sftpProperties);
			log.debug("remote path = {}", sftpProperties.getPath());
			fileList = ftpUtil.getAllFileOrderByDateAsc(sftpProperties.getPath(), prefix);
		} else {
			throw new ConfigurationException("Sftp的配置信息在数据库中不存在");
		}
	}

	/*
	 * @return localFilePath - path where unzipped files located.
	 */
	public String unzipFile(SftpFileDto sftpFileDto) {
		if (null != channelSftp && channelSftp.isConnected()) {
			// 获取临时目录
			 String tmpPath = commonsService.getCodeValueByCodeTypeAndCode("tmpPath",
			 "tmpPath");
//			String tmpPath = "d:/temp/";
			String localFilePath = tmpPath + "ServiceReminder/" + sftpFileDto.getFileName()
					+ DateFormatUtils.format(new Date(), "yyyyMMdd_HHmmssSSS");
			String localFile = localFilePath + "/" + sftpFileDto.getFileName();
			File file = new File(localFilePath);
			if (!file.exists()) {
				file.mkdirs();
			}
			ftpUtil.downloadFile(sftpFileDto.getFileName(), sftpProperties.getPath(), localFile);
			// 解压缩
			ZipFileUtils.unZip(localFile, localFilePath);
			return localFilePath;
		}
		return null;
	}

	public static void removeFile(SftpFileDto sftpFileDto) {
		 ftpUtil.remove(sftpProperties.getPath() + sftpFileDto.getFileName());
	}
}
